﻿using System.Collections.Generic;
using System.Text;
using System.Web.Mvc;
using Nop.Core;
using Nop.Plugin.Payments.PayPalHosted.Models;
using Nop.Plugin.Payments.PayPalHosted.Services;
using Nop.Services.Configuration;
using Nop.Services.Orders;
using Nop.Services.Payments;
using Nop.Web.Framework.Controllers;
using System.Linq;

namespace Nop.Plugin.Payments.PayPalHosted.Controllers
{
    public class PaymentPayPalHostedController : BaseNopPaymentController
    {
        private readonly IPayPalIPNService _payPalIPNService;
        private readonly PayPalHostedPaymentSettings _payPalHostedPaymentSettings;
        private readonly ISettingService _settingService;
        private readonly IPayPalInterfaceService _payPalInterfaceService;
        private readonly IPayPalRequestService _payPalRequestService;
        private readonly IOrderService _orderService;
        private readonly IPayPalHostedService _payPalHostedService;

        public PaymentPayPalHostedController(IPayPalIPNService payPalIPNService,
        PayPalHostedPaymentSettings payPalHostedPaymentSettings,
        ISettingService settingService, IPayPalInterfaceService payPalInterfaceService,
        IPayPalRequestService payPalRequestService,
        IOrderService orderService,
        IPayPalHostedService payPalHostedService)
        {
            _payPalIPNService = payPalIPNService;
            _payPalHostedPaymentSettings = payPalHostedPaymentSettings;
            _settingService = settingService;
            _payPalInterfaceService = payPalInterfaceService;
            _payPalRequestService = payPalRequestService;
            _orderService = orderService;
            _payPalHostedService = payPalHostedService;
        }

        public override IList<string> ValidatePaymentForm(FormCollection form)
        {
            var warnings = new List<string>();
            return warnings;
        }

        public override ProcessPaymentRequest GetPaymentInfo(FormCollection form)
        {
            var paymentInfo = new ProcessPaymentRequest();
            return paymentInfo;
        }

        [AdminAuthorize]
        [ChildActionOnly]
        public ActionResult Configure()
        {
            var model = new ConfigurationModel
                            {
                                ApiSignature = _payPalHostedPaymentSettings.ApiSignature,
                                DoNotHaveBusinessAccount =
                                    _payPalHostedPaymentSettings.DoNotHaveBusinessAccount,
                                EmailAddress = _payPalHostedPaymentSettings.EmailAddress,
                                EnableDebugLogging = _payPalHostedPaymentSettings.EnableDebugLogging,
                                IsLive = _payPalHostedPaymentSettings.IsLive,
                                Password = _payPalHostedPaymentSettings.Password,
                                Username = _payPalHostedPaymentSettings.Username,
                                HandlingFee = _payPalHostedPaymentSettings.HandlingFee,
                                HandlingFeeIsPercentage = _payPalHostedPaymentSettings.HandlingFeeIsPercentage,
                                BusinessId = _payPalHostedPaymentSettings.BusinessId
                            };
            return View("Nop.Plugin.Payments.PayPalHosted.Views.PaymentPayPalHosted.Configure", model);
        }

        [HttpPost]
        [AdminAuthorize]
        [ChildActionOnly]
        public ActionResult Configure(ConfigurationModel model)
        {
            if (!ModelState.IsValid)
                return Configure();
            _payPalHostedPaymentSettings.ApiSignature = model.ApiSignature;
            _payPalHostedPaymentSettings.DoNotHaveBusinessAccount = model.DoNotHaveBusinessAccount;
            _payPalHostedPaymentSettings.EmailAddress = model.EmailAddress;
            _payPalHostedPaymentSettings.EnableDebugLogging = model.EnableDebugLogging;
            _payPalHostedPaymentSettings.IsLive = model.IsLive;
            _payPalHostedPaymentSettings.Password = model.Password;
            _payPalHostedPaymentSettings.Username = model.Username;
            _payPalHostedPaymentSettings.HandlingFee = model.HandlingFee;
            _payPalHostedPaymentSettings.HandlingFeeIsPercentage = model.HandlingFeeIsPercentage;
            _payPalHostedPaymentSettings.BusinessId = model.BusinessId;

            _settingService.SaveSetting(_payPalHostedPaymentSettings);

            return View("Nop.Plugin.Payments.PayPalHosted.Views.PaymentPayPalHosted.Configure", model);
        }

        [ChildActionOnly]
        public ActionResult PaymentInfo()
        {
            return View("Nop.Plugin.Payments.PayPalHosted.Views.PaymentPayPalHosted.PaymentInfo");
        }

        [ValidateInput(false)]
        public ActionResult Redirect()
        {
            return View("Nop.Plugin.Payments.PayPalHosted.Views.PaymentPayPalHosted.Redirect");
        }

        [ValidateInput(false)]
        public ActionResult IPNHandler()
        {
            byte[] param = Request.BinaryRead(Request.ContentLength);
            string ipnData = Encoding.ASCII.GetString(param);

            _payPalIPNService.HandleIPN(ipnData);

            //nothing should be rendered to visitor
            return Content("");
        }

        [ValidateInput(false)]
        public ActionResult Return(FormCollection form)
        {
            var orderId = _payPalHostedService.HandlePayment(form.AllKeys.ToDictionary(s => s, s => form[s]));

            return RedirectToRoute("CheckoutCompleted", new { orderId });
        }
    }
}